#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "time.h"
#include "CoreGame.h"
#define Map_size 10
#define false 0
#define true 1

void InitPlayer(Player *player)
{
    player->monery=0;
    player->HP=100;
    player->attack=10;
    player->x=0;
    player->y=0;
}

int InitShore(Shore *shore)
{

    shore->name1=10;
    shore->value1=400;
    shore->name2=50;
    shore->value2=200;
    shore->namea.string="宝剑";
    shore->nameb.string="药品";
}

int GetRandNumber(int max)
{
    return rand() % max;
}

Monster* CreateMonster(const char*name,int hp,int attack)
{
    Monster* monster=(Monster*)malloc(sizeof(Monster));
    if(monster==NULL)
    {
        printf("monster mallooc error!\n");
        return NULL;
    }
    Init_string(&monster->name,name);
    monster->HP=hp;
    monster->attack=attack;
    monster->x=GetRandNumber(Map_size);
    monster->y=GetRandNumber(Map_size);
    return monster;
}

Treasure*CreateTreasure(const char *name,int value,int sign)
{
    Treasure *treasure=(Treasure*)malloc(sizeof(Treasure));
    if(treasure==NULL)
    {
        printf("treasure malloc error\n");
        return NULL;
    }
    Init_string(&treasure->name,name);
    treasure->value=value;
    treasure->sign=sign;
    treasure->x=GetRandNumber(Map_size);
    treasure->y=GetRandNumber(Map_size);
    return treasure;
}

DynamicArray *CreateDynamicAarray(DynamicArray *array,int  number)
{
    array->dp=malloc(sizeof(void*)*number);
    if(array->dp==NULL)
    {
        printf("DynamicArray malloc error\n");
        return NULL;
    }
    array->size=number;
    array->len=0;
}

int InitMonsters(DynamicArray *monster)
{
    if(Initialize(monster) == false)
    {
        printf("InitMonsters Error!\n");
        return false;    
    }    
    FILE *fd2=fopen("./Data/monster.txt","r");
    for(int i=0;i<=36;i++)
    {
        char name[10];
        int hp;
        int attack;
        fscanf(fd2,"%s %d %d",name,&hp,&attack);
        InsertArray(monster,CreateMonster(name,hp,attack));
    }
    fclose(fd2);
    return true;
}

int InitTreasure(DynamicArray *treasure)
{
    if(Initialize(treasure)==false)
    {
        return false;
    }
    FILE *fd3=fopen("./Data/treasure.txt","r");
    for(int i=0;i<=19;i++)
    {
        char name[10];
        int value;
        int sign;
        fscanf(fd3,"%s %d %d",name,&value,&sign);
        InsertArray(treasure,CreateTreasure(name,value,sign));
    }
    fclose(fd3);
    return true;
}

void MakeMove(Player *player,int dx,int dy)
{
    player->x+=dx;
    player->y+=dy;
    if(player->x<0)
        player->x=0;
    if(player->x>=Map_size)
        player->x=Map_size-1;
    if(player->y<0)
        player->y=0;
    if(player->y>=Map_size)
        player->y=Map_size-1;
}

int Battle(Player *player,Monster *monster)
{
    system("clear");
    int i=monster->HP;
    printf("您遭遇到了%s 怪兽血量%d 攻击力%d\n",monster->name.string,monster->HP,monster->attack);
    
    while(1)
    {
        printf("请选择您的行动 1、攻击 2、防御 3、逃跑\n");
        printf("请选择:");
        int  choice=0;
        scanf(" %d",&choice);
        switch(choice)
        {
            case  1:
                printf("你对怪兽造成了%d伤害\n",player->attack);
                if(GetRandNumber(2)==1)
                {
                    printf("怪兽对你进行了还击，造成了%d伤害\n",monster->attack);
                    player->HP-=monster->attack;
                }
                monster->HP-=player->attack;
                break;
            case  2:
                printf("怪兽对你进行了攻击\n");
                if(GetRandNumber(3)!=1)
                {
                    printf("你成功防御住了怪兽的攻击\n");
                }
                else
                {
                    printf("防御失败，怪兽对你造成了%d伤害\n",monster->attack);
                    player->HP-=monster->attack;
                }
                break;
            case 3:
                if(GetRandNumber(2)==1)
                {
                    printf("逃跑成功\n");
                    monster->HP=0;
                    return true;
                }
                else
                {
                    printf("逃跑失败\n");
                    printf("怪兽对你进行了还击，造成了%d伤害\n",monster->attack);
                    player->HP-=monster->attack;
                }
                break;
            default:
                break;
        }
        if(player->HP<=0)
        {
            printf("怪兽打败了你\n");
            return false;
        }
        if(monster->HP<=0)
        {
            player->monery+=i*2;
            printf("你战胜了怪兽\n");
            printf("你获得了%d金钱\n",i*2);
            return true;
        }
        printf("当前你的血量%d\n",player->HP);
        printf("当前怪物血量%d\n",monster->HP);
    }
}

void PersonalAttibutes(Player *player)
{
    while(1)
    {
        char a[1]={0};
        memset(a,0,1);
        read(0,a,1);
        printf("生命值：%d\n",player->HP);
        printf("攻击力：%d\n",player->attack);
        printf("金钱：%d\n",player->monery);
        printf("                                         按't'退出\n");
        if(a[0]=='t')
        {
            system("clear");
            return ;
        }
    }
}

void StoreCenter(Shore *shore,Player *player)
{
    while(1)
    {
        char a[1]={0};
        memset(a,0,1);
        read(0,a,1);
        printf("%s :   价格：%d    攻击力加%d\n",shore->namea.string,shore->value1,shore->name1);
        printf("                            购买请按'1'\n");
        printf("%s :   价格：%d    生命力加%d\n",shore->nameb.string,shore->value2,shore->name2);
        printf("                            购买请按'2'\n");
        printf("                                          按't'退出\n");
        if(a[0]=='t')
        {
            system("clear");
            return ;
        }
        if(a[0]=='1'&&player->monery>=shore->value1)
        {
            player->monery-=shore->value1;
            player->attack+=shore->name1;
            printf("购买成功！\n");
        }
        else if(a[0]=='2'&&player->monery>=shore->value2)
        {
            player->monery-=shore->value2;
            player->HP+=shore->name2;
            printf("购买成功！\n");
        }
    }
}
void Map(Player *player)
{
    printf("  ");
    for(int i=1;i<=10;i++)
    {
        printf("%d ",i);
    }
    printf("\n");
    for(int i=0;i<Map_size;i++)
    {
        printf("%2d ",i+1);
        for(int j=0;j<Map_size;j++)
        {
            if(player->x==j&&player->y==i)
            {
                printf("X ");
            }
            else
            {
                printf("- ");
            }
        }
        printf("\n");
    }
    
}
